package com.example.test_01.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.test_01.R;
import com.example.test_01.model.Food;
import com.example.test_01.service.FoodService;
import com.example.test_01.service.FoodServiceImpl;

import java.util.List;

public class FoodOrderAdapter extends RecyclerView.Adapter<FoodOrderAdapter.ViewHolder>{

    private List<Food> list;
    private final FoodService service = new FoodServiceImpl();

    private void list(){
        list = service.list();
    }
    public FoodOrderAdapter(List<Food> list){ this.list = list; }
    public void reload(List<Food> list){
        this.list = list;
        notifyDataSetChanged();
    }
    public double getSum(){
        double sum = 0;
        for(int i = 0; i < list.size(); i++){
            sum+=list.get(i).getPrice();
        }
        return sum;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.food_order,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Food food = list.get(position);
        holder.text_food_name.setText(food.getName() + "      " + food.getPrice() + '￥');
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView text_food_name;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            text_food_name = itemView.findViewById(R.id.text_food_name);
        }
    }
}
